//
//  PicView.m
//  UI06_UIScrollView_UIPageControl
//
//  Created by Scott on 16/5/10.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "PicView.h"
#import "PictureScrollView.h"

@interface PicView () <UIScrollViewDelegate>

@property (nonatomic, retain) UIPageControl *page;

@property (nonatomic, retain) PictureScrollView *picScroll;

@end

@implementation PicView

- (void)dealloc {
    [_page release];
    [_picScroll release];
    [super dealloc];
}

#pragma mark - override 
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        // 初始化子控件.
        self.picScroll = [[PictureScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:self.picScroll];
        [_picScroll release];
        
        self.picScroll.delegate = self;
        
        
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 40, CGRectGetWidth(self.bounds), 40)];
    
        [self addSubview:self.page];
        [_page release];
        
        [self.page addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventValueChanged];
        
        
    }
    return self;
}

#pragma mark - page
- (void)handlePage:(UIPageControl *)page {
    
    // 设置scoll.contentOffset偏移量.
    [self.picScroll setContentOffset:CGPointMake(page.currentPage * CGRectGetWidth(self.bounds), 0) animated:YES];
    
}


#pragma mark - scrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 设置page.currentPage
    self.page.currentPage = scrollView.contentOffset.x / CGRectGetWidth(self.bounds);
    
    
}



#pragma mark -
- (void)addPictures:(NSArray<UIImage *> *)pictures {
    
    [self.picScroll addPictures:pictures];
    
    self.page.numberOfPages = pictures.count;
    
}


@end
